package com.jprocms.module.cms.dal.mysql.accessstatistics;

import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.mybatis.core.mapper.BaseMapperX;
import com.jpro.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.jprocms.module.cms.controller.admin.accessstatistics.vo.AccessStatisticsListReqVO;
import com.jprocms.module.cms.controller.admin.accessstatistics.vo.AccessStatisticsPageReqVO;
import com.jprocms.module.cms.dal.dataobject.accessstatistics.AccessStatisticsDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.lang.Nullable;

import java.util.List;

/**
 * 访问统计 Mapper
 *
 * @author jprocms
 */
@Mapper
public interface AccessStatisticsMapper extends BaseMapperX<AccessStatisticsDO> {

    default PageResult<AccessStatisticsDO> selectPage(AccessStatisticsPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AccessStatisticsDO>()
                .eqIfPresent(AccessStatisticsDO::getSiteId, reqVO.getSiteId())
                .likeIfPresent(AccessStatisticsDO::getStatName, reqVO.getStatName())
                .eqIfPresent(AccessStatisticsDO::getPvCount, reqVO.getPvCount())
                .eqIfPresent(AccessStatisticsDO::getUvCount, reqVO.getUvCount())
                .eqIfPresent(AccessStatisticsDO::getIpCount, reqVO.getIpCount())
                .eqIfPresent(AccessStatisticsDO::getNvCount, reqVO.getNvCount())
                .eqIfPresent(AccessStatisticsDO::getBounceCount, reqVO.getBounceCount())
                .eqIfPresent(AccessStatisticsDO::getAccessDuration, reqVO.getAccessDuration())
                .eqIfPresent(AccessStatisticsDO::getDateString, reqVO.getDateString())
                .eqIfPresent(AccessStatisticsDO::getStatType, reqVO.getStatType())
                .orderByAsc(AccessStatisticsDO::getDateString));
    }

    default List<AccessStatisticsDO> selectList(AccessStatisticsListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<AccessStatisticsDO>()
                .eqIfPresent(AccessStatisticsDO::getSiteId, reqVO.getSiteId())
                .likeIfPresent(AccessStatisticsDO::getStatName, reqVO.getStatName())
                .eqIfPresent(AccessStatisticsDO::getPvCount, reqVO.getPvCount())
                .eqIfPresent(AccessStatisticsDO::getUvCount, reqVO.getUvCount())
                .eqIfPresent(AccessStatisticsDO::getIpCount, reqVO.getIpCount())
                .eqIfPresent(AccessStatisticsDO::getNvCount, reqVO.getNvCount())
                .eqIfPresent(AccessStatisticsDO::getBounceCount, reqVO.getBounceCount())
                .eqIfPresent(AccessStatisticsDO::getAccessDuration, reqVO.getAccessDuration())
                .eqIfPresent(AccessStatisticsDO::getDateString, reqVO.getDateString())
                .eqIfPresent(AccessStatisticsDO::getStatType, reqVO.getStatType())
                .orderByAsc(AccessStatisticsDO::getDateString));
    }


    /**
     * 按日期统计
     *
     * @param siteId 站点ID
     * @param type   类型
     * @param begin  开始日期
     * @param end    介绍日期
     * @return 统计结果
     */
    List<AccessStatisticsDO> statByDate(@Param("siteId") Long siteId, @Param("type") Integer type,
                                        @Nullable @Param("begin") String begin, @Nullable @Param("end") String end);

    /**
     * 删除指定日期字符串之前的数据
     *
     * @param dateString 指定的日期字符串
     * @return 被删除的数据条数
     */
    int deleteBeforeDateString(@Param("dateString") String dateString);

    /**
     * 根据日期字符串删除数据
     *
     * @param dateString 日期字符串
     * @param type       统计类型
     * @return 被删除的数据条数
     */
    int deleteByDateString(@Param("dateString") String dateString, @Nullable @Param("type") Integer type);

    /**
     * 根据站点ID删除数据
     *
     * @param siteId 站点ID
     * @return 被删除的数据条数
     */
    int deleteBySiteId(@Param("siteId") Long siteId);
}
